package com.zzmcc.common;


import com.alibaba.fastjson.JSONObject;
import com.zzmcc.base.BaseController;
import com.zzmcc.sys.model.Userinfo;
import util.ChristStringUtil;



public class IndexController extends BaseController {


	//主页
	public void index(){
		if (getSessionAttr("loginUser")==null){
			redirect("/login");
		}else{
			redirect("/main");
		}

	}
	
	public void login(){

		render("common/login.html");
	}



	public void validateLogin(){
		Userinfo user = getModel(Userinfo.class, "userinfo");

		Userinfo loginUser = Userinfo.dao.findFirst("select * from sys_userinfo where username=?  and pwd =? ",
				user.get("username"),ChristStringUtil.md5((String) user.get("pwd")));

		if (null != loginUser){
			setSessionAttr("loginUser",loginUser);
			renderDWZSuccessJson("登录成功！");
			//redirect("/main");

		}else {
			//没有权限 
			//e.printStackTrace();
			renderDWZErrorJson("登录失败！");
		} 
	}
	
	//跳转修改密码页面
	public void toUpdatePassword(){
		Userinfo loginUser = (Userinfo) getSessionAttr("loginUser");
		if (null!=loginUser) {
			String username = loginUser.get("username");
			setAttr("username", username);
		}
		render("common/updatePassword.html");
	}

	//修改密码
	public void updatePassword(){
		String username = getPara("username");
		Userinfo loginUser = Userinfo.dao.findFirst("select * from sys_userinfo where username=?", username);
		String newPwd = getPara("newPassword");
		String oldPwd=getPara("oldPassword");
		String md5OldPwd = ChristStringUtil.md5(oldPwd);
		String md5NewPwd = ChristStringUtil.md5(newPwd);
		if(loginUser.get("pwd").equals(md5OldPwd)){
			loginUser.set("pwd", md5NewPwd).update();
			renderDWZSuccessJson("密码修改成功，新密码已生效！");
		}else{
			renderDWZErrorJson("您输入的旧密码错误！");
		}
	}
	
	public void main(){
		if (getSessionAttr("loginUser")==null){
			redirect("/login");
		}else {
			Userinfo loginUser = (Userinfo) getSessionAttr("loginUser");
			setAttr("loginUser", loginUser);
			render("common/main.html");
		}

	}
	public void sidebar(){
		String func = getPara("func");
		render("common/sidebar_"+func+".html");
	}
	public void shortcut(){
		render("common/shortcut.html");
	}

	
	public void logout(){

		removeSessionAttr("loginUser");
		redirect("/index");

	}
}
